package com.isunland.managesystem.ui;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.SendMessage;
import com.isunland.managesystem.utils.LogUtil;
import java.util.HashMap;
import org.json.JSONException;

public class SendMassageFragment extends Fragment {
  protected static final String TAG = "SendMassageFragment";
  private BaseVolleyActivity mActivity;
  private EditText et_massage_people;
  private EditText et_massage_organize;
  private String code;
  private SendMessage sendMessage;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {

    // // View widget = inflater.inflate(R.layout.fragment_sendmassage,
    // container,
    // false);
    //
    // mActivity = (BaseVolleyActivity) getActivity();
    //
    // et_massage_people = (EditText) widget
    // .findViewById(R.id.et_massage_people);
    //
    // et_massage_organize = (EditText) widget
    // .findViewById(R.id.et_massage_organize);
    //
    // return widget;
    return null;
  }

  @Override public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
  }

  @Override public void onResume() {

    volleyPostCustomerSendMessage();

    et_massage_people.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View arg0) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        AlertDialog mAlertDialog = builder.create();

        mAlertDialog.show();
        mAlertDialog.getWindow().setContentView(R.layout.dialog_reveicepeople);

        ListView lv_dialogreveice = (ListView) mAlertDialog.findViewById(R.id.lv_dialogreveice);
        lv_dialogreveice.setAdapter(new MyAdapter());
      }

      class MyAdapter extends BaseAdapter {

        @Override public int getCount() {

          return 0;
        }

        @Override public Object getItem(int arg0) {
          return arg0;
        }

        @Override public long getItemId(int arg0) {
          return arg0;
        }

        @Override public View getView(int arg0, View arg1, ViewGroup arg2) {

          View.inflate(getActivity(), R.layout.lv_sendmassage_dialog, null);
          return null;
        }
      }
    });

    super.onResume();
  }

  public static SendMassageFragment newInstance(String string) {
    return new SendMassageFragment();
  }

  private void volleyPostCustomerSendMessage() {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_SENDMASSAGE_PEOPLE);

    // 参数是写死的.需要选择 部门,和人员.传id和name
    HashMap<String, String> params = new HashMap<String, String>();
    CurrentUser mUser = CurrentUser.newInstance(getActivity());
    code = mUser.getMemberCode();
    params.put("type", "user");
    params.put("orgCode", code);

    // params.put("receiverName", "");
    // params.put("receiverOrgId", "10000040270013");
    // params.put("receiverOrgName", "产品二部");

    LogUtil.i(TAG, "params=" + params.toString());

    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        resolveResponse(arg0);
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  private void resolveResponse(String arg0) {

    try {

      sendMessage = new Gson().fromJson(arg0, SendMessage.class);

      if (sendMessage == null || sendMessage.getResult() == null) {

        return;
      }

      if (!sendMessage.getResult().equals("1")) {

        // 发送失败

        return;
      }

      // LogUtil.i(TAG, "sendMessageResult=" + sendMessage.getResult());
    } catch (Exception e) {
      LogUtil.e(TAG, "Exception", e);
    }
  }
}
